﻿using System;
using System.Collections;
using System.Configuration;
using System.Data; 
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;  
using Microsoft.JCI.Common.Entity;
using Microsoft.JCI.Bll;
using System.Collections.Generic;

namespace Microsoft.JCI.Portal.UserControl
{
    public partial class ProductCtrl : BaseControl
    {
        public Guid ProductID
        {
            get
            {
                if (ViewState["Product"] == null)
                {
                    return Guid.Empty;
                }

                return new Guid(ViewState["Product"].ToString());
            }
            set
            {
                ViewState["Product"] = value;
            }
        }

        private string _Enabled = "Enabled";
        public bool Enabled
        {
            get
            {
                if (ViewState[_Enabled] != null)
                {
                    return Convert.ToBoolean(ViewState[_Enabled]);
                }
                return true;
            }
            set
            {
                ViewState[_Enabled] = value;
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                SystemBll bll = new SystemBll();

                //产品分类
                List<SysDictionaryEntity> list = bll.GetDictionaryListFromCache(Microsoft.JCI.Common.Utility.Enumerator.DictionaryType.ProductCategory);
                ddlCategory.DataSource = list;
                ddlCategory.DataTextField = "ValueCN";
                ddlCategory.DataValueField = "DictionaryID";
                ddlCategory.DataBind();

                //产品品牌
                list = bll.GetDictionaryListFromCache(Microsoft.JCI.Common.Utility.Enumerator.DictionaryType.ProductBrand);
                ddlBrand.DataSource = list;
                ddlBrand.DataTextField = "ValueCN";
                ddlBrand.DataValueField = "DictionaryID";
                ddlBrand.DataBind();

                //制冷机类型
                //list = bll.GetDictionaryListFromCache(Microsoft.JCI.Common.Utility.Enumerator.DictionaryType.RefrigerationType);
                //ddlRefrigerationType.DataSource = list;
                //ddlRefrigerationType.DataTextField = "ValueCN";
                //ddlRefrigerationType.DataValueField = "DictionaryID";

                ////产品等级
                //list = bll.GetDictionaryListFromCache(Microsoft.JCI.Common.Utility.Enumerator.DictionaryType.ProductLevel);
                //ddlProductLevel.DataSource = list;
                //ddlProductLevel.DataTextField = "ValueCN";
                //ddlProductLevel.DataValueField = "DictionaryID";

                DisabledForm();
            }
        }

        public void LoadProduct()
        {
            MasterDataBll masterDataBll = new MasterDataBll();
            MadProductEntity entity = null;
            if (ProductID != Guid.Empty)
            {
                entity = masterDataBll.Get<MadProductEntity>(ProductID);
                if (entity == null) return;
               
            }

            if(entity.ProductCategory != Guid.Empty)
                ddlCategory.SelectedValue = entity.ProductCategory.ToString();

            if (entity.ProductBrand != Guid.Empty)
                ddlBrand.SelectedValue = entity.ProductBrand.ToString();

            txtProductType.Text = entity.ProductType;
            if(entity.BuyDate != DateTime.MinValue)
                RadDatePicker_BuyDate.SelectedDate = entity.BuyDate;

            txtSerialNumber.Text = entity.SerialNumber;
            txtInvoiceCode.Text = entity.InvoiceCode;
            txtInternCode.Text = entity.InternCode;
            txtOutSideCode.Text = entity.OutSideCode;

            ddlRefrigerationType.SelectedValue = entity.RefrigerationType;

            txtCardCode.Text = entity.CardCode;
 
            txtProductLevel.Text = entity.ProductLevel;

            if(entity.ProductDate != DateTime.MinValue)
            RadDatePicker_ProductDate.SelectedDate = entity.ProductDate;

            txtSelPhone.Text = entity.SelPhone;
            txtProductRemark.Text = entity.Remark;
        }

        public MadProductEntity GetEntity()
        {
            MasterDataBll masterDataBll = new MasterDataBll();
            if (ProductID != Guid.Empty)
            {
                MadProductEntity entity = masterDataBll.Get<MadProductEntity>(ProductID);
                if (entity == null)
                {
                    entity = new MadProductEntity();
                }

                if (ddlCategory.SelectedValue != string.Empty)
                    entity.ProductCategory = new Guid(ddlCategory.SelectedValue);

                if(ddlBrand.SelectedValue != string.Empty)
                    entity.ProductBrand = new Guid(ddlBrand.SelectedValue) ;

                entity.ProductType = txtProductType.Text;

                if (RadDatePicker_BuyDate.SelectedDate.HasValue)
                    entity.BuyDate = RadDatePicker_BuyDate.SelectedDate.Value;

                entity.SerialNumber = txtSerialNumber.Text  ;
                entity.InvoiceCode = txtInvoiceCode.Text;
                entity.InternCode = txtInternCode.Text;
                entity.OutSideCode = txtOutSideCode.Text;

                entity.RefrigerationType = ddlRefrigerationType.SelectedValue;

                txtCardCode.Text = entity.CardCode;
                entity.ProductLevel = txtProductLevel.Text;

                if (RadDatePicker_ProductDate.SelectedDate.HasValue)
                    entity.ProductDate = RadDatePicker_ProductDate.SelectedDate.Value;

                entity.SelPhone = txtSelPhone.Text;
                entity.Remark = txtProductRemark.Text;
                return entity;
            }
            else
            {
                MadProductEntity entity = new MadProductEntity();
                if (ddlCategory.SelectedValue != string.Empty)
                    entity.ProductCategory = new Guid(ddlCategory.SelectedValue);

                if (ddlBrand.SelectedValue != string.Empty)
                    entity.ProductBrand = new Guid(ddlBrand.SelectedValue);

                entity.ProductType = txtProductType.Text;

                if (RadDatePicker_BuyDate.SelectedDate.HasValue)
                    entity.BuyDate = RadDatePicker_BuyDate.SelectedDate.Value;

                entity.SerialNumber = txtSerialNumber.Text;
                entity.InvoiceCode = txtInvoiceCode.Text;
                entity.InternCode = txtInternCode.Text;
                entity.OutSideCode = txtOutSideCode.Text;

                entity.RefrigerationType = ddlRefrigerationType.SelectedValue;

                txtCardCode.Text = entity.CardCode;
                entity.ProductLevel = txtProductLevel.Text;

                if (RadDatePicker_ProductDate.SelectedDate.HasValue)
                    entity.ProductDate = RadDatePicker_ProductDate.SelectedDate.Value;

                entity.SelPhone = txtSelPhone.Text;
                entity.Remark = txtProductRemark.Text;
                entity.CreateBy = base.CurrentUserEntity.Account;
                entity.CreateOn = DateTime.Now;
                return entity;

            }

        }

        private void DisabledForm()
        {
            if (!Enabled)
            {
                ddlBrand.Enabled = false;
                ddlCategory.Enabled = false;
                txtProductType.Enabled = false;
                txtSelPhone.Enabled = false;
                RadDatePicker_BuyDate.Enabled = false;
                txtSerialNumber.Enabled = false;
                txtInvoiceCode.Enabled = false;
                txtInternCode.Enabled = false;
                txtOutSideCode.Enabled = false;
                ddlRefrigerationType.Enabled = false;
                txtCardCode.Enabled = false;
                txtProductLevel.Enabled = false;
                RadDatePicker_ProductDate.Enabled = false;
                txtProductRemark.Enabled = false;
            }
            else
            {
                ddlBrand.Enabled = true;
                ddlCategory.Enabled = true;
                txtProductType.Enabled = true;
                txtSelPhone.Enabled = true;
                RadDatePicker_BuyDate.Enabled = true;
                txtSerialNumber.Enabled = true;
                txtInvoiceCode.Enabled = true;
                txtInternCode.Enabled = true;
                txtOutSideCode.Enabled = true;
                ddlRefrigerationType.Enabled = true;
                txtCardCode.Enabled = true;
                txtProductLevel.Enabled = true;
                RadDatePicker_ProductDate.Enabled = true;
                txtProductRemark.Enabled = true;
            }
        }

    }
}